<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Category extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('category.index');
    }

    public function getCategory() {
        $res = DB::table('category')->paginate(10)->toArray();
        if ($res) {
            return response()->json([
                'code'=>0, 
                'count'=>$res['total'],
                'data'=>$res['data']
            ]);
        } else {
            return response()->json([
                'code'=>2, 
                'msg'=>'请求失败',
            ]);
        }
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('category.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->only('cat_name', 'cat_sort');
        $cat = DB::table('category')->where('cat_name', $data['cat_name'])->first();
        if ($cat) {
            return returnStatus('fail', '添加失败,已存在分类');
        }
        $res = DB::table('category')->insert($data);
        if ($res) {
            return returnStatus('success', '添加成功');
        } else {
            return returnStatus('fail', '添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $category = DB::table('category')->where('id', $id)->first();
        return view('category.edit', ['category'=>$category]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->only('cat_name', 'cat_sort');
        $res = DB::table('category')->where('id', $id)->update($data);
        if ($res) {
            return returnStatus('success', '编辑成功');
        } else {
            return returnStatus('fail', '编辑失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('category')->where('id', $id)->delete();

        if ($res) {
            return returnStatus('success', '删除成功');
        } else {
            return returnStatus('fail', '删除失败');
        }
    }
}
